<template>
    <scroll-view class="areamain">
        <select-classify-item
            v-for="(item, index) in datas"
            :item="item"
            :key="index"
            @selectFn="selectFn"></select-classify-item>
    </scroll-view>
</template>

<script>
import { GetCountrywideTree } from "@/service/api/web.js"
import selectClassifyItem from "@/components/select/select-classify-item.vue"
import { getProductTypeTree } from "@/service/api/product.js"
import { GetGoodGroupTree} from "@/service/api/erp.js"
export default {
    components: {
        selectClassifyItem,
    },
    data() {
        return {
            datas: [],
            regionData: [],
        }
    },
    onLoad(e) {
        if (e.regionData) {
            this.regionData = JSON.parse(e.regionData)
			console.log(123)
			console.log(this.regionData)
        }
    },
    mounted() {
		this.getProductTypeTree()
    },
    methods: {
		
		getProductTypeTree() {
		    let param = {
		        pageSize: 99,
		        pageNumber: 1,
				fullName:''
		    }
			uni.showLoading({
			    title: "加载中",
			})
		    GetGoodGroupTree(param).then((res) => {
				if(res.State==1){
					this.displayCategories(res.Data);
					setTimeout(e => {
						this.datas = res.Data
						this.$nextTick(()=>{
							uni.hideLoading()
						})
					},200)
				}else{
					uni.hideLoading()
					setTimeout(e => {
						this.easyToast(res.Msg)
					},1000)
				}
		
		    })
		},
		displayCategories(categories, level = 1) {
			  categories.forEach(category => {
				if(this.regionData.indexOf(category.id) >=0){
					category.checked = true
				}else{
					category.checked = false
				}
				if (category.children && category.children.length > 0) {
				  this.displayCategories(category.children, level + 1);
				}
			  });
		},
        selectFn(data) {
            const eventChannel = this.getOpenerEventChannel()
            eventChannel.emit("getSelectData", {
                data,
            })
            this.easyGoBack()
        },
        onNavigationBarButtonTap(e) {
			console.log(222)
			console.log(this.datas)
            this.regionData = []
            this.displayRegionData(this.datas);
			setTimeout(e => {
				this.selectFn(this.regionData)
			},200)
        },
		displayRegionData(categories, level = 1) {
		  categories.forEach(category => {
			  if(category.checked == true){
				  let obj = {
					  name:category.name,
					  id:category.id
				  }
				  this.regionData.push(obj)
			  }
		    if (category.children && category.children.length > 0) {
		      this.displayRegionData(category.children, level + 1);
		    }
		  });
		},
    },
}
</script>

<style lang="scss" scoped>
.areamain {
    margin-top: 12rpx;
    background: #ffffff;
}
</style>
